<?php
namespace app\models;

use Yii;

class CouponTpl extends Basic
{
    
    public static function tableName()
    {
        return '{{%coupon_tpl}}';
    }

    public function rules()
    {
        return [
            ['comid', 'required', 'message' => 'comid不能为空', 'on' => 'add'],
            ['comid', 'string', 'message' => 'comid必须是字符串',
                      'min' => 8, 'tooShort' => 'comid最短8位',
                      'max' => 8, 'tooLong' => 'comid最长8位', 'on' => 'add'],
            ['couponname', 'required', 'message' => 'couponname不能为空', 'on' => 'add'],
            ['couponname', 'string', 'message' => 'couponname必须是字符串', 
                         'max' => 128, 'tooLong' => 'couponname最长128位'],
            ['flag', 'in', 'range' => ['1', '2', '3'],  'message' => 'flag格式不正确'],
            ['condition_amount', 'integer', 'message' => 'condition_amount必须为数字，或数字字符串', 
                          'min'=>0, 'tooSmall' => 'condition_amount最小存储0', 
                          'max'=>65535, 'tooBig' => 'condition_amount最大存储65535'],
            ['amount', 'integer', 'message' => 'amount必须为数字，或数字字符串', 
                          'min'=>0, 'tooSmall' => 'amount最小存储0', 
                          'max'=>65535, 'tooBig' => 'amount最大存储65535'],
            ['discount', 'number', 'message' => 'discount格式不正确'],
            // ['remark', 'default', 'value' => ''],
            ['remark', 'string', 'message' => 'remark必须是字符串', 
                         'max' => 256, 'tooLong' => 'remark最长256位'],
            ['circulation', 'integer', 'message' => 'circulation必须为数字，或数字字符串', 
                          'min'=>1, 'tooSmall' => 'circulation最小存储1', 
                          'max'=>65535, 'tooBig' => 'circulation最大存储65535'],
            // ['receive_num', 'default', 'value' => 0],
            ['receive_num', 'integer', 'message' => 'receive_num必须为数字，或数字字符串', 
                          'min'=>0, 'tooSmall' => 'receive_num最小存储0', 
                          'max'=>65535, 'tooBig' => 'receive_num最大存储65535'],
            ['begin_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['begin_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'begin_time非yyyy-MM-dd HH:mm:ss格式'],       
            ['end_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['end_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'end_time非yyyy-MM-dd HH:mm:ss格式'],
            ['add_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['add_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'add_time非yyyy-MM-dd HH:mm:ss格式'],
        ];
    }


    /*添加*/
    public function addTpl($data, $comid)
    {
        $data['CouponTpl']['comid'] = $comid;
        $data['CouponTpl']['begin_time'] = $data['CouponTpl']['validityTime'][0] . ' 00:00:00';
        $data['CouponTpl']['end_time'] = $data['CouponTpl']['validityTime'][1] . ' 23:59:59';
        $data['CouponTpl']['add_time'] = date('Y-m-d H:i:s', time());
        // P($data);
        $this->scenario = 'add';
        if($this->load($data) and $this->validate()){
            if($this->save(false)){
                // $id = $this->getPrimaryKey();
                return true;
            }
        }
        return false;
    }

    /*
    修改
    $data   修改的数据
    $id     主键
    */
    public function modifyTpl($data, $id)
    {
        $data['CouponTpl']['begin_time'] = $data['CouponTpl']['validityTime'][0] . ' 00:00:00';
        $data['CouponTpl']['end_time'] = $data['CouponTpl']['validityTime'][1] . ' 23:59:59';
        $couponTpl = CouponTpl::find()->where('cid = :id', [':id' => $id])->one();
        // P($data);
        if(!is_null($couponTpl)){
            if($this->load($data) and $this->validate()){
                if(isset($this->couponname)){
                    $couponTpl->couponname = $this->couponname;
                }
                if(isset($this->flag)){
                    $couponTpl->flag = $this->flag;
                }
                if(isset($this->remark)){
                    $couponTpl->remark = $this->remark;
                }
                if(isset($this->condition_amount)){
                    $couponTpl->condition_amount = $this->condition_amount;
                }
                if(isset($this->amount)){
                    $couponTpl->amount = $this->amount;
                }
                if(isset($this->discount)){
                    $couponTpl->discount = $this->discount;
                }
                if(isset($this->circulation)){
                    $couponTpl->circulation = $this->circulation;
                }
                if(isset($this->receive_num)){
                    $couponTpl->receive_num = $this->receive_num;
                }
                if(isset($this->begin_time)){
                    $couponTpl->begin_time = $this->begin_time;
                }
                if(isset($this->end_time)){
                    $couponTpl->end_time = $this->end_time;
                }
                if($couponTpl->save(false)){
                    return true;
                }
            }
        };
        return false;
    }

    
}